package com.dmall.graph.dfs.app;

import com.dmall.graph.Graph;

import java.util.ArrayList;

public class AllPairsPath {

    private Graph G;
    private SingleSourcePath[] paths;

    public AllPairsPath(Graph G) {
        this.G = G;
        this.paths = new SingleSourcePath[G.V()];
        for (int i = 0; i < G.V(); i++) {
            this.paths[i] = new SingleSourcePath(G, i);
        }
    }

    public boolean isConnected(int v, int w) {
        G.validateVertex(v);
        G.validateVertex(w);
        return paths[v].isCollectedTo(w);
    }

    public Iterable<Integer> path(int s, int t) {
        ArrayList<Integer> res = new ArrayList<>();
        if (!isConnected(s, t)) {
            return res;
        }
        return paths[s].path(t);
    }

    public static void main(String[] args) {
        String filename = AllPairsPath.class.getClassLoader().getResource("g.txt").getPath();
        Graph g = new Graph(filename);
        AllPairsPath allPairsPath = new AllPairsPath(g);
        System.out.println("0 -> 6 : " + allPairsPath.path(0, 6));
        System.out.println("0 -> 5 : " + allPairsPath.path(0, 5));
    }
}
